﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
using Exhibition.Common.Extension;
namespace Exhibition.Dao
{

    public class MessageInfoDao
    {


        public MessageInfoDao()
        {

            Mapper.CreateMap<E_MessageInfo, MessageInfoModel>();
        }
        public List<E_MessageInfo> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_MessageInfo.Count();

                return edm.E_MessageInfo.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public MessageInfoModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_MessageInfo.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_MessageInfo, MessageInfoModel>(query);
            }

        }


        public int Save(MessageInfoModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_MessageInfo.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_MessageInfo();

                    edm.AddToE_MessageInfo(query);

                }


                query.MsgName = obj.MsgName;

                query.MsgContent = obj.MsgContent;

                query.CreateUserId = obj.CreateUserId;

                query.CreateTime = obj.CreateTime;

                query.GetUserId = obj.GetUserId;

                query.UserType = obj.UserType;
                query.DelUserId = obj.DelUserId;

                edm.SaveChanges();

                return query.Id;


            }

        }

        public List<MessageInfoModel> GetList(string tit,string curid,int stid, int startIndex, int maxRow, out int record)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                var query = from s in edm.E_MessageInfo
                            select s;
                if (!string.IsNullOrEmpty(tit)) {
                    query = query.Where(s=>s.MsgName.Contains(tit));
                }
                if (!string.IsNullOrEmpty(curid))
                {
                    query = query.Where(s => !s.DelUserId.Contains(curid) || s.DelUserId ==null);
                }
                if (stid > 0) {
                    query = query.Where(s=>s.SiteId==stid);
                }

                var str = query.ToTraceString();

                record = query.Count();
                return Mapper.Map<List<E_MessageInfo>, List<MessageInfoModel>>(query.OrderByDescending(s=>s.Id).Skip(startIndex).Take(maxRow).ToList());
            }
        }



        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_MessageInfo.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

